<?php
/* 
 * TO DO - currently subtitles are turned off if they are not the "identifier" (eg if accessed through via storyboard)
 * - not sure that this wld be correct if the program did have subtitles - ??
 * 
 * display videoplayer with one or more marked timein events, jump to first event
 * EITHER take query_string time_in passing time - accepts value in secords or time  xx:xx:xx[,xxx] format
 * and ou.ProductionNumber and show video in player window
 * OR take - search=ou.SubtitleText:pinatubo AND program=XX and ou.Identifier=XX 
 * eg
 * ?search=ou.SubtitleText:pinatubo&program=S808P&identifier=Subtitle
 * a search phrase passed and produce an array of results  to CREATE
 * an image for the timeline
 * THEN
 * Search on - ou.ProductionNumber:S808P and filter by PID:tech* to get ou.VersionLocation
 * transform version location to the rtmp URL
 */
// initialise some interface variables
# play lead in before search result?
$preplay_time= 4;
$pixel_start= 28;
$pixel_end= 388;
$adjust= 6;

  $search_string= strip_tags($_GET['search']);
  $search_phrase= strip_tags($_GET['timein']);
  if(isset($_GET['program'])){
    $program_id = strip_tags($_GET['program']);
  }else{
    $program_id='';
  }


# process timein
if(!preg_match('/timein/',$_SERVER['QUERY_STRING'])){
  $timein=0; 
  # if there's no timein there should be an identifier
# let identifier default to subtitle
    $identifier="";

}else{
  $timein= strip_tags($_GET['timein']);
  $timein= convert_to_secs($timein);
  $play_start_secs=intval($timein);
/*  if(preg_match('/\d+?:\d+:\d/', $timein)){
    $sep=":";
    $time_array= explode($sep, $timein);
    $play_start_secs= $time_array[0]*360 + $time_array[1]*60 + (floor(0.5 + str_replace(',',".",$time_array[2])));
    $timein= $play_start_secs;
  }else{
    $play_start_secs=intval($timein);
  }*/
$single_flag_location= flag_location($timein_converted, $duration, $pixel_start, $pixel_end, $adjust);
$final_gif ="/sites/all/modules/custom/videofinder/flag_bin/" . $single_flag_location . ".gif";
$final_gif_filepath= "/var/www/html" . $final_gif;
if(!file_exists($final_gif_filepath)){
exec("touch $final_gif 2>&1 > /dev/null");
exec("/usr/bin/composite /var/www/html/sites/all/modules/custom/videofinder/just_flag.gif -geometry +$single_flag_location+0 $final_gif_filepath $final_gif_filepath 2>&1 > /dev/null");
}

}
if($play_start_secs > $preplay_time){
$play_start_secs= ($play_start_secs-$preplay_time);
}

$file_path = "sites/default/files/videos/programs/" . $program_id . ".flv";
$file_path= get_location($program_id);
$file_path = preg_replace('/^\//','',$file_path);
# ou.ProductionNumber:S808P AND ou.Identifier:Subtitle AND ou.SubtitleText:volcano
#$duration = convert_to_secs(solr_onefield_search("ou.ProductionNumber",$program . " AND PID:tech*" ,"ou.Duration"));
$duration= get_duration($program_id);
$duration= convert_to_secs($duration);
$stream_path = convert_stream_path($file_path);
$bg_image = position_flag($play_start_secs, $duration, $pixel_start, $pixel_end, $adjust);

$caption_file="http://videofinder.open.ac.uk/sites/default/files/subtitles/" . $program_id . ".srt";
// function to return which flag to use for control background there's an adjust because the pole for flag1 can be at 5 pixels...

function position_flag($timein_secs, $duration, $pixel_start, $pixel_end, $adjust){
  $total_width = $pixel_end - $pixel_start;
  $secs_per_pixel = $total_width/$duration;
  $image_location = $pixel_start + round($secs_per_pixel*$timein_secs) - $adjust ;
  $image = "/sites/all/modules/custom/videofinder/flags/flag_" . $image_location . ".gif";
return $image;
}

function get_duration($program_id){
$sql = "SELECT duration from videofinder_program where nid=" . $program_id ;
$link = mysql_connect("aacsmysqlcluster.open.ac.uk", "voyager", "v0yag3r") or die(mysql_error()); # host user password
mysql_select_db('ava_website',$link);
$result = mysql_query($sql,$link);
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
  $duration= $row['duration'];
}
return $duration;
}

function get_location($program_id){
$sql = "SELECT location from videofinder_media where fkey_program=" . $program_id . " AND (media='flv' OR media='f4v') AND (bap_number IS NULL OR bap_number=0)";
$link = mysql_connect("aacsmysqlcluster.open.ac.uk", "voyager", "v0yag3r") or die(mysql_error()); # host user password
mysql_select_db('ava_website',$link);
$result = mysql_query($sql,$link);
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
  $location= $row['location'];
}
return $location;
}

# this just locates the position of an image
function flag_location($timein_secs, $duration, $pixel_start, $pixel_end,$adjust){
  $total_width = $pixel_end - $pixel_start;
  $secs_per_pixel = $total_width/$duration;
  $image_location = $pixel_start + round($secs_per_pixel*$timein_secs) - $adjust ;
return $image_location;
}

function convert_to_secs($timein){
if(preg_match('/\d+:\d+,\d+/', $timein)){
  $sep=":";
  $time_array= explode($sep, $timein);
  $time_seconds= $time_array[0]*360 + $time_array[1]*60 + (floor(0.5 + str_replace(',',".",$time_array[2])));
}elseif(preg_match('/\d+\'\s?\d+\'/', $timein)){
  $sep="'";
  $time_array= explode($sep, $timein);
  $time_seconds= $time_array[0]*60 + $time_array[1];
}elseif(preg_match('/\d+/', $timein)){
  $time_seconds= $timein;
}
return $time_seconds;
}

function convert_stream_path($file_location){
$pattern='/http:\/\/videofinder\.open\.ac\.uk/';
$replace='';
$file_location= preg_replace($pattern, $replace, $file_location);
if(preg_match('/f4v/', $file_location)===1){
  $replace= 'rtmp://videofinder.open.ac.uk:1935/$1/mp4:$2';
}else{
  $replace= 'rtmp://videofinder.open.ac.uk:1935/$1/$2';
}
$pattern='/sites\/default\/files\/videos\/(programs|rushes)\/(\d+\.f(4|l)v)/';
$new_path = preg_replace( $pattern,$replace,$file_location);
return $new_path;
}

# determine if subtitles are on 
# subtitles are never on if they don't exist
# but you can set if they default to on or not
function are_subtitles_on($program_id, $default_subs_on=TRUE){
if($default_subs_on===TRUE){
# are there subtitles?
  if(file_exists("/var/www/html/sites/default/files/subtitles/$program_id.srt")){
    $subtitles_on=TRUE;
  }else{
    $subtitles_on=FALSE;
  }
}else{
  $subtitles_on=FALSE;
}
return $subtitles_on;
}
?><!DOCTYPE html>
<!-- 
	This is Flowplayer standalone demo. The fastest way to get started. 
	You can freely copy things on your site. All demos can be found from: 
	
	http://flowplayer.org/demos/
	
	By default flowplayer expects a flowplayer.controls.swf of the same version
	as the flowplayer.swf in the same folder
-->
<head>
<!-- flowplayer scripts should be loaded from your servers NOT from static.flowplayer.org  -->
<script type="text/javascript" src="http://videofinder.open.ac.uk/flowplayer/flowplayer.min.js"></script>
<link type="text/css" rel="stylesheet" href="/floatbox/floatbox.css" />
<script type="text/javascript" src="/floatbox/floatbox.js"></script>
<script type="text/javascript" src="/floatbox/options.js"></script>

</head>
<body style="margin:0; padding:0;">
<!-- player container-->
<?php 
$video_output .= "<a href=\"http://videofinder.open.ac.uk/programs/" . $program_id . ".mp4\" 
	style=\"display:block;width:400px;height:350px;\" 
	id=\"player\">";
$video_output .= "</a>";

#<!-- this script block will install Flowplayer inside previous A tag -->

$video_output .="<script language=\"JavaScript\">\n";
$video_output .="	flowplayer(\"player\", \"http://videofinder.open.ac.uk/flowplayer/flowplayer.swf\",{\n";
# resize the video and switch the background image
$video_output .= "onFullscreen:function(){this.getPlugin('controls').css({background:'url(/sites/all/modules/custom/videofinder/fs_flag_end.gif)',height:52,width:850,bottom:10,align:'center',body:{fontSize:10,fontFamily:'Arial',textAlign:'center',fontWeight: 'bold'}})},\n";
#$video_output .= "onFullscreen:function(){this.getPlugin('controls').css({background:'none'});},";
  $video_output .= "onFullscreenExit:function(){this.getPlugin('controls').css({background:'url(" . $bg_image . ")',height:26,width:400,body:{fontSize:12,fontFamily:'Arial',textAlign:'center'}})},\n";
if($program_id>0){
$video_output .= "onFullscreen:function(){this.getPlugin('content').css({height:100,width:1200,bottom:10,align:'center',body:{fontSize:10,fontFamily:'Arial',textAlign:'center',fontWeight: 'bold'}})},\n";
$video_output .= "onFullscreenExit:function(){this.getPlugin('content').css({height:50,width:295,body:{fontSize:12,fontFamily:'Arial',textAlign:'center'}})},\n";
$video_output .= "log: { level: 'debug', filter: 'org.flowplayer.*' },\n";
}

$video_output .="	clip: {\n";
$video_output .="		url: '" . $stream_path . "',\n";
if($program_id>0){
if(are_subtitles_on($program_id)){
    $video_output .=     "captionUrl: '" . $caption_file . "',\n";
  }
}
# NOTE: scaling:'fit' maintains proportions whilst scaling: 'scale' will distort to try and fill the window
$video_output .="		scaling:'fit',\n";
#$video_output .="		autoPlay:false,\n";
$video_output .="// configure clip to use influxis as our provider, it uses our rtmp plugin\n";
$video_output .="		provider: 'open_uni',\n";
$video_output .="//		start:500,\n";
$video_output .="		height: 300,";
$video_output .="		onStart: function() {this.seek(" . $play_start_secs . ");},\n";
if($program_id>0){
#  if($identifier==="subtitle"){
      $video_output .="		onLoad: function() {this.getPlugin('captions').css({opacity: 0});}\n";
#  }
}
$video_output .="		\n";
$video_output .="	},	\n";
$video_output .="	plugins: {\n";
$video_output .="		open_uni: {\n";
$video_output .="			url: 'http://videofinder.open.ac.uk/flowplayer/flowplayer.rtmp.swf',\n";
$video_output .="			netConnectionUrl: 'rtmp://videofinder.open.ac.uk:1935/programs'},\n";
$video_output .="	// The controlbar is called \"controls\". By tweaking this, you can modify its look and feel\n";
if($program_id>0){
  if(are_subtitles_on($program_id)){
    $video_output .="		captions: { url: 'http://videofinder.open.ac.uk/flowplayer/flowplayer.captions.swf',\n";
    $video_output .="			captionTarget: 'content',\n";
    $video_output .="			button: {bottom:46,right:22}\n";
    $video_output .="			},\n";
    $video_output .=           "content: { url: 'http://videofinder.open.ac.uk/flowplayer/flowplayer.content.swf', bottom: 45, height:50,";
    $video_output .=           " width:295, backgroundColor: '#000000', backgroundGradient: '1', border: 0, textDecoration: 'none', ";
    $video_output .=           " style: { ";
    $video_output .=                 "body: { fontSize: 12, fontFamily: 'Arial',fontWeight: 'bold', textAlign: 'center', color: '#ffffff'} ";
    $video_output .=                  " } ";
    $video_output .=          "},";
  }
}
# slow motion works but is slow to start and does not work for f4v files - 
if(stristr($stream_path,'.flv')!==FALSE){
$video_output .="		slowmotion: { url: 'http://videofinder.open.ac.uk/flowplayer/flowplayer.slowmotion.swf'},";
}
// add a content plugin to show speed information (this is optional)
$video_output .= "		speedIndicator: {";
$video_output .= "			url: 'http://videofinder.open.ac.uk/flowplayer/flowplayer.content.swf',";
$video_output .= "						bottom: 50,";
$video_output .= "						right: 15,";
$video_output .= "						width: 135,";
$video_output .= "						height: 30,";
$video_output .= "						border: 'none',";
$video_output .= "						style: { ";
$video_output .= "							body: { ";
$video_output .= "								fontSize: 12, ";
$video_output .= "								fontFamily: 'Arial',";
$video_output .= "								textAlign: 'center',";
$video_output .= "								color: '#ffffff'";
$video_output .= "							}";
$video_output .= "						},";
$video_output .= "backgroundColor: 'rgba(20, 20, 20, 0.5)',";
#$video_output .= "			// we don't want the plugin to be displayed by default, ";
#$video_output .= "			// only when a speed change occur.";
$video_output .= "			display: 'none'";
$video_output .= "				},";
// end speed indicator

/* enable this or the double controls below...
$video_output .="	controls: {\n";
$video_output .="\n";
$video_output .="		// location of the controlbar plugin\n";
$video_output .="		url: 'http://videofinder.open.ac.uk/flowplayer/flowplayer.controls.swf',\n";
# Firefox does not display this - cross-site problem(?)
$video_output .=		"background: 'url(" . $bg_image . ") no-repeat',width:425,";
#$video_output .=		"background: false,";
#$video_output .=		"width: 425,";
$video_output .="backgroundColor: \"transparent\",\n";
#video_output .="backgroundColor: '#ffffff',\n";
$video_output .="			backgroundGradient: \"none\",\n";
$video_output .="		// display properties such as size, location and opacity\n";
$video_output .="		top: 260,\n";
$video_output .="		left: 0,\n";
$video_output .="		bottom: 0,\n";
$video_output .="\n";
$video_output .="\n";
$video_output .="		// styling properties (will be applied to all plugins)\n";
$video_output .="\n";
$video_output .="\n";
$video_output .="		// controlbar specific settings\n";
$video_output .="		timeColor: '#ffffff',\n";
$video_output .="		durationColor: '#ffffff',\n";
#$video_output .="		sliderColor: '#ffffff',\n";
$video_output .="		volumeColor: '#000000',\n";
#$video_output .="		timeBorderRadius: 2,\n";
#$video_output .="		timeBgColor: '#555555',\n";
$video_output .="		bufferColor: '#990000',\n";
$video_output .="		play: true,\n";
$video_output .="		scrubber: true,\n";
$video_output .="		timeBgColor: null,\n";
$video_output .="		scrubberHeightRatio: 0.6,\n";
#$video_output .="		mute: false,\n";
#$video_output .="		volume: false,\n";
// the fastBackward and/or slowBackward buttons are disabled because there is currently no
// ability to seek back beyond start of stream with these buttons
#$video_output .="		fastBackward: true,\n";
$video_output .="\n";
$video_output .="		// tooltips (since 3.1)\n";
$video_output .="			tooltips: {\n";
$video_output .="			buttons: true,\n";
$video_output .="			fullscreen: 'Enter fullscreen mode'\n";

// a little more styling 			
//$video_output .="		width: '80%', ";
//$video_output .="		bottom: 5, ";
//$video_output .="		left: '50%', ";
//$video_output .="		borderRadius: 15";
$video_output .="\n";
$video_output .="		}\n";
$video_output .="	}\n";
*/

$video_output .="	controls: {\n";
$video_output .="\n";
$video_output .="		// location of the controlbar plugin\n";
$video_output .="		url: 'http://videofinder.open.ac.uk/flowplayer/flowplayer.controls.swf',\n";
# Firefox does not display this - cross-site problem(?)
$video_output .=		"background: 'none',";

$video_output .="		backgroundColor: \"transparent\",\n";
$video_output .="			backgroundGradient: \"none\",\n";
$video_output .="		// display properties such as size, location and opacity\n";
#$video_output .="		top: 260,\n";
$video_output .="		left: 35,\n";
$video_output .="		bottom: 22,\n";
$video_output .="\n";
$video_output .="\n";
$video_output .="		// styling properties (will be applied to all plugins)\n";
$video_output .="\n";
$video_output .="\n";
$video_output .="		// controlbar specific settings\n";
$video_output .="		timeColor: '#ffffff',\n";
$video_output .="		durationColor: '#ffffff',\n";
#$video_output .="		sliderColor: '#ffffff',\n";
$video_output .="		volumeColor: '#000000',\n";
#$video_output .="		timeBorderRadius: 2,\n";
#$video_output .="		timeBgColor: '#555555',\n";
#$video_output .="		bufferColor: '#990000',\n";
#$video_output .="		slider: false,";
$video_output .="		play: true,\n";
$video_output .="		scrubber: false,\n";
$video_output .="		timeBgColor: null,\n";
$video_output .="		scrubberHeightRatio: 0.6,\n";
#$video_output .="		mute: false,\n";
#$video_output .="		volume: false,\n";
// the fastBackward and/or slowBackward buttons are disabled because there is currently no
// ability to seek back beyond start of stream with these buttons
#$video_output .="		fastBackward: true,\n";
$video_output .="\n";
$video_output .="		// tooltips (since 3.1)\n";
$video_output .="			tooltips: {\n";
$video_output .="			buttons: true,\n";
$video_output .="			fullscreen: 'Enter fullscreen mode'\n";
// a little more styling 			
//$video_output .="		width: '80%', ";
//$video_output .="		bottom: 5, ";
//$video_output .="		left: '50%', ";
//$video_output .="		borderRadius: 15";
$video_output .="\n";
$video_output .="		}\n";
$video_output .="	}\n";

// the timeline
$video_output .="	,timeline_control: {\n";
$video_output .="\n";
$video_output .="		// location of the controlbar plugin\n";
$video_output .="		url: 'http://videofinder.open.ac.uk/flowplayer/flowplayer.controls.swf',\n";
# Firefox does not display this - cross-site problem(?)
if($identifier==="Subtitle"){
  $video_output .=		"background: 'url(" . $final_gif . ") no-repeat',width:400,";
}else{
  $video_output .=		"background: 'url(" . $bg_image . ") no-repeat',width:400,";
}
#$video_output .=		"background: false,";
#$video_output .=		"width: 425,";
$video_output .="backgroundColor: \"transparent\",\n";
#video_output .="backgroundColor: '#ffffff',\n";
$video_output .="			backgroundGradient: \"none\",\n";
$video_output .="		// display properties such as size, location and opacity\n";
#$video_output .="		top: 280,\n";
$video_output .="		left: 0,\n";
$video_output .="		bottom: 0,\n";
$video_output .="\n";
$video_output .="\n";
$video_output .="		// styling properties (will be applied to all plugins)\n";
$video_output .="\n";
$video_output .="\n";
$video_output .="		// controlbar specific settings\n";
$video_output .="		timeColor: '#ffffff',\n";
$video_output .="		durationColor: '#ffffff',\n";
$video_output .="		sliderColor: '#ffffff',\n";
$video_output .="		volumeColor: '#000000',\n";
#$video_output .="		timeBorderRadius: 2,\n";
#$video_output .="		timeBgColor: '#555555',\n";
$video_output .="		bufferColor: '#990000',\n";
$video_output .="		play: true,\n";
$video_output .="		scrubber: true,\n";
$video_output .="		timeBgColor: null,\n";
$video_output .="		scrubberHeightRatio: 0.6,\n";
// bar heigth ratio is ratio of button to scrubber height
// values nearer 1 are wider scrubbers & thinner buttons
$video_output .="		scrubberBarHeightRatio: 0.2,\n";
$video_output .="		buttonColor: '#ffffff',\n";
$video_output .="		buttonHeight: 0.4,\n";

$video_output .="		play: false,\n";
$video_output .="		slowForward: false,\n";
$video_output .="		fastForward: false,\n";
$video_output .="		time: false,\n";
$video_output .="		duration: false,\n";
$video_output .="		mute: false,\n";
$video_output .="		volume: false,\n";
$video_output .="		fullscreen: false,\n";
// prevent hiding of timeline_control
#$video_optput .="		mouseOutDelay: false\n";
$video_output .="		autoHide: false,\n";
// the fastBackward and/or slowBackward buttons are disabled because there is currently no
// ability to seek back beyond start of stream with these buttons
#$video_output .="		fastBackward: true,\n";
$video_output .="\n";
$video_output .="		// tooltips (since 3.1)\n";
$video_output .="			tooltips: {\n";
$video_output .="			buttons: true,\n";
$video_output .="			fullscreen: 'Enter fullscreen mode'\n";
// a little more styling 			
//$video_output .="		width: '80%', ";
//$video_output .="		bottom: 5, ";
//$video_output .="		left: '50%', ";
//$video_output .="		borderRadius: 20";
$video_output .="\n";
$video_output .="		}\n";
$video_output .="	}\n";


$video_output .="}\n";
// start canvas
$video_output .=", canvas: {width:400, height:350}\n";
$video_output .="\n";
$video_output .=",screen: {\n";
$video_output .="width:400, height:320, top:0, right:0\n";
$video_output .="}\n";
$video_output .="\n";
$video_output .="	\n";
$video_output .="	});\n";
$video_output .="</script>\n";
echo $video_output;
?>
</body></html>